﻿using Microsoft.AspNetCore.Mvc;
using MiniFox.Infrastructure;
using MiniFox.Platform.Applications.Models;
using MiniFox.Resources;
using MiniFox.Utility;
using MiniFox.Web.Infrastructure;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace MiniFox.Platform.Applications.Web
{
    /// <summary>
    /// 
    /// </summary>
    [PublicAccessible, NoCache]
    public class OrganizationController : AuthorizedController<IOrganizationService>
    {
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public Response<IEnumerable<Organization>> Query()
        {
            IEnumerable<Organization> orgs = this.Component.GetOrganizations();
            return orgs.CreateResponse();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="orgId"></param>
        /// <param name="deptId"></param>
        /// <returns></returns>
        [HttpGet]
        public Response<ITreeNode> Depts([FromQuery(Name = GlobalKey.ORG_ID), Required(ErrorMessageResourceType = typeof(CommonMessages), ErrorMessageResourceName = "MSG_CommonRequired")] string orgId,
            [Required(ErrorMessageResourceType = typeof(CommonMessages), ErrorMessageResourceName = "MSG_CommonRequired")] string deptId)
        {
            var tree = Component.GetDepts(orgId, deptId);
            return tree.CreateResponse();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="orgId"></param>
        /// <returns></returns>
        [HttpGet]
        public Response<IEnumerable<Dept>> UserDepts([FromQuery] string? orgId = null)
        {
            var depts = Component.GetUserDepts(orgId);
            return depts.CreateResponse();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="orgId"></param>
        /// <returns></returns>
        [HttpGet]
        public Response<IEnumerable<Dept>> ManagedDepts([FromQuery] string? orgId = null)
        {
            var depts = Component.GetManagedDepts(orgId);
            return depts.CreateResponse();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public Response<IEnumerable<ITreeNode>> Tree()
        {
            var tree = Component.DeptTreeObject.Result;
            return tree.CreateResponse();
        }
    }
}
